<%@page import="cn.edu.nju.ws.sview.views.Group"%>
<%@page import="cn.edu.nju.ws.sview.wyge.relations.adataset.relations.Relation"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%
	String user = (String) session.getAttribute("user");
	String ugname = (String) session.getAttribute("groupName");
	if (user == null || ugname == null) {
%>
<script language="javascript">
	window.self.close();
</script>
<%
	return;
	}
	
	String title = "Relation";
	String lang = request.getParameter("lang");
		if (lang != null && lang.equals("zh")) {
			request.getRequestDispatcher("relation_create_edit_zh.jsp").forward(
					request, response);
				return;
		} else if (lang == null) {
			lang = "en";
		}
		int ugid = Group.getGroupIdByName(ugname);
	//String group_id = request.getParameter("gid");
	
	/* if(group_id != null){
		request.getSession().setAttribute("gid", group_id);
	} */
	String purpose = request.getParameter("purpose");
	if (purpose != null && purpose.equals("edit")) {
		title += " Edit";
	} else if (purpose != null && purpose.equals("new")) {
		title += " Create";
	} else {
	
%>
<script language="javascript">
	window.self.close();
</script>
<%
	return;
	}
%>
<title><%=title%></title>
<link rel="icon" href="/sview/favicon.ico" />
<link rel="stylesheet" type="text/css"
	href="/sview/css/smoothness/jquery-ui-1.8.16.custom.css" />
<link rel="stylesheet" type="text/css" href="/sview/css/layout.css" />
<link rel="stylesheet" type="text/css"
	href="/sview/css/relations/relation_create_edit.css" />
<script type="text/javascript" src="/sview/script/jquery.js"></script>
<script type="text/javascript"
	src="/sview/script/jquery-ui-1.8.16.custom.min.js"></script>
<script type="text/javascript" src="/sview/script/global.js"></script>
<script type="text/javascript" src="/sview/script/relations/jquery.cookie.js"></script>
<script type="text/javascript"
	src="/sview/script/relations/relation_create_edit.js"></script>
</head>
<body>
	<%
		String ds_type = request.getParameter("ds_type");
		int ds_id = Integer.parseInt(request.getParameter("ds_id"));
		int relation_id = 0;
		String group_id = null;
		if (purpose.equals("edit")) {
			relation_id = Integer.parseInt(request
					.getParameter("relation_id"));
			group_id = String.valueOf(Group.getGroupIdByName(Relation.getInstance(relation_id).getGroup()));//String.valueOf(Group.getGroupIdByName(((Relation)request.getSession().getAttribute("relation"+relation_id)).getGroup()));
			if(group_id==null || Integer.valueOf(group_id)!=ugid){
				System.err.println("In relation_create_edit.jsp: illegal ids!!  \tgid:"+group_id+"|"+ugid);
				String location = "/sview/relations/index.jsp";
				if (lang.equals("zh"))
					location = location + "?lang=" + lang;
				response.sendRedirect(location);
				return;
			}
		}
	%>
  <jsp:include page="/vheader.jsp" flush="true">
     <jsp:param name="f" value="d"/>
  </jsp:include>
	<div class="main">
		<div id="rel_info">
			<div id="rel_title"></div>
			<div id="rel_meta"></div>
		</div>
		<div id="tabs">
			<ul>
				<li><a href="#tabs-1">Pattern Selection</a></li>
				<li><a href="#tabs-2" id="tabs_2_role">Role Selection</a></li>
			</ul>
			<div id="tabs-1">
				<div>
					<jsp:include page="rel_pattern.jsp" flush="true">
						<jsp:param name="ds_type" value="<%=ds_type%>" />
						<jsp:param name="ds_id" value="<%=ds_id%>" />
						<jsp:param name="relation_id" value="<%=relation_id%>" />
						<jsp:param name="purpose" value="<%=purpose%>" />
						<jsp:param name="user" value="<%=user%>" />
						<jsp:param name="lang" value="<%=lang %>" />
					</jsp:include>
				</div>
			</div>
			<div id="tabs-2">
				<div>
					<jsp:include page="rel_role.jsp" flush="true">
						<jsp:param name="ds_type" value="<%=ds_type%>" />
						<jsp:param name="ds_id" value="<%=ds_id%>" />
						<jsp:param name="relation_id" value="<%=relation_id%>" />
						<jsp:param name="purpose" value="<%=purpose%>" />
						<jsp:param name="user" value="<%=user%>" />
						<jsp:param name="lang" value="<%=lang %>" />
					</jsp:include>
				</div>
			</div>
		</div>
	</div>
	<input type="hidden" id="ds_type" value="<%=ds_type%>" />
	<input type="hidden" id="ds_id" value="<%=ds_id%>" />
	<input type="hidden" id="user" value="<%=user%>" />
	<input type="hidden" id="gid" value="<%=group_id %>"/>
	<input type="hidden" id="relation_id" value="<%=relation_id%>" />
	
	<div id="dialog_target">
		<div id="onloading" class="section">
			<div style="text-align: center">
				<p>&nbsp;</p>
				<p>Loading Data, Please Wait...</p>
			</div>
		</div>
		<div id="selExpand" class="section"></div>
	</div>
	 <%-- <input type="hidden" id="lang" value="<%=lang %>" /> --%>
	 <input type="hidden" id="display-lang" value="en" />
	 <jsp:include page="/vfooter.jsp"></jsp:include>
</body>
</html>